﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CorporateGym.DAL;
using BLL.Entities;

namespace BLL.Managers
{
    public class MessagesManager
    {
        private static MessagesManager m_This;
        private static readonly object sr_SyncObj = new Object();
        public static MessagesManager Instance
        {
            get
            {
                if (m_This == null)
                {
                    lock (sr_SyncObj)
                    {
                        if (m_This == null)
                        {
                            m_This = new MessagesManager();
                        }
                    }
                }

                return m_This;
            }
        }

        private MessagesManager()
        {
        }

        public void PostMessageToMessageBoard(int workerId, string messageSubject, string messageContent)
        {
            DataAccess.Instance.AddMessageboardMessage(-1, workerId, DateTime.Now ,messageSubject, messageContent, true);
        }

        public List<Message> GetMessageBoardMessages()
        {
            List<Message> messageBoardMessages = new List<Message>();
            List<CorporateGymDB.MessageRow> messageBoardMessageRow = DataAccess.Instance.GetMessageBoardMessageRow();

            foreach (CorporateGymDB.MessageRow messageRow in messageBoardMessageRow)
            {
                Message message = Message.CreateFromRow(messageRow);
                messageBoardMessages.Add(message);
            }

            return messageBoardMessages;
        }

        public void SendMessage(int workerId, string messageSubject, string messageContent, List<int> recipientsWorkerIdList)
        {
            // ParentMessageId??
            DataAccess.Instance.AddMessage(-1, workerId, DateTime.Now, messageSubject, messageContent, recipientsWorkerIdList, false);
        }


        public List<Message> GetWorkerIncomingMessages(int workerId)
        {
            List<Message> workerMessages = new List<Message>();
            List<CorporateGymDB.MessageRow> workerMessageRowsList = DataAccess.Instance.GetAllMessagesToWorker(workerId);

            foreach (CorporateGymDB.MessageRow messageRow in workerMessageRowsList)
            {
                Message messageToAdd = Message.CreateFromRow(messageRow);
                workerMessages.Add(messageToAdd);
            }

            return workerMessages;
        }

        public List<Message> GetWorkerOutgoingMessages(int workerId)
        {
            List<Message> workerOutgoingMessages = new List<Message>();

            List<CorporateGymDB.MessageRow> workerMessageRowsList = DataAccess.Instance.GetAllMessagesFromWorker(workerId);
            foreach (CorporateGymDB.MessageRow messageRow in workerMessageRowsList)
            {
                Message messageToAdd = Message.CreateFromRow(messageRow);
                workerOutgoingMessages.Add(messageToAdd);
            }

            return workerOutgoingMessages;
        }

        public List<GymWorker> GetMessageReceivers(int messageId)
        {
            List<GymWorker> receiversList = new List<GymWorker>();

            List<CorporateGymDB.GymWorkerRow> recieversRowList = DataAccess.Instance.GetMessageRecievers(messageId);
            foreach (CorporateGymDB.GymWorkerRow gymWorkerRow in recieversRowList)
            {
                GymWorker workerToAdd = GymWorker.CreateFromRow(gymWorkerRow);
                receiversList.Add(workerToAdd);
            }

            return receiversList;
        }

        public void BroadcastMessage(string messageSubject, string messageContent)
        {
            List<Trainee> trainees = TraineeManager.Instance.GetTrainees();
            string[] emails = new string[trainees.Count];
            for (int i = 0; i < trainees.Count; i++)
            {
                emails[i] = trainees[i].Email;
            }

            EmailService.SendEmailBcc(messageSubject, messageContent, emails);
        }

        public void sendEmailToRecipients(List<String> i_Emails, string i_Subject, string i_Message)
        {
            EmailService.SendEmailBcc(i_Subject, i_Message, i_Emails.ToArray());
        }
    }
}
